package com.metagurukul.informer.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import com.metagurukul.informer.dao.LoginUserDAO;
import com.metagurukul.informer.valueObjects.Users;

public class LoginService {

	private String username="";
	private String password="";
	LoginUserDAO loginuserdao;
	public LoginService(String username,String password) {
	
		this.username=username;
		this.password=password;
	}
	/**
	 * this method fetch logged in user detail and send information that
	 * if logged in person is an admin or user
	 * @return
	 * @throws SQLException
	 */
	public String[] doLogin() throws SQLException {
		String result[]=new String[2];
		loginuserdao=new LoginUserDAO();
		Users users= loginuserdao.LoginUser(username, password);
		if(users!=null){
			if(users.isAdmin()){
				result[0]="admin";
				result[1]=users.getName();
			}else{
				result[0]="user";
				result[1]=users.getName();
			}
		}
		return result;
	}
	/**
	 * check that if user is member or not
	 * @return
	 * @throws SQLException
	 */
	public boolean isUserExist() throws SQLException{
		loginuserdao=new LoginUserDAO();
		boolean isExist=false;
		isExist=loginuserdao.isExistingUser(username);
		return  isExist;
	}
	/**
	 * check that if user is registered or not
	 * @return
	 * @throws SQLException
	 */
	public boolean isTempUser()throws SQLException{
		loginuserdao=new LoginUserDAO();
		boolean isExist=false;
		isExist=loginuserdao.isTempUser(username);
		return  isExist;
	}
	/**
	 * make updation of a registered user to as member when he logging in first time 
	 * @throws SQLException
	 */
	public void finalRegisterUser() throws SQLException{
		loginuserdao=new LoginUserDAO();
		loginuserdao.finalRegistration(username);
		
	}
	
}
